package com.hang.controller;

import com.hang.common.Result;
import com.hang.exception.MyException;
import com.hang.model.dto.base.ConditionDTO;
import com.hang.model.dto.request.DocumentDeleteDTO;
import com.hang.model.dto.request.DocumentModelDTO;
import com.hang.model.dto.request.ModuleConditionDTO;
import com.hang.model.dto.response.DocumentQueryListVO;
import com.hang.model.vo.DocumentDetailVO;
import com.hang.model.vo.DocumentInfoVO;
import com.hang.service.ITbDocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "文档管理模块接口")
@RequestMapping("document")
@RestController
@Slf4j
public class DocumentController {
    @Resource
    private ITbDocumentService documentService;

    @ApiOperation(value = "新增文档记录")
    @PostMapping("create")
    public Result<Object> createDocument(
            @RequestBody @Valid DocumentModelDTO documentModelDTO
    ){
        try {
            documentService.createDocument(documentModelDTO);
            return Result.ok().message("创建成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "修改文档记录")
    @PostMapping("update")
    public Result<Object> updateDocument(
            @RequestBody @Valid DocumentModelDTO documentModelDTO
    ){
        try {
            documentService.updateDocument(documentModelDTO);
            return Result.ok().message("修改成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "批量删除文档记录")
    @DeleteMapping("deleteList")
    public Result<Object> deleteDocument(
            @RequestBody List<DocumentDeleteDTO> documentDeleteDTOList
    ){
        try {
            documentService.deleteDocumentList(documentDeleteDTOList);
            return Result.ok().message("删除成功");
        }catch (MyException me){
            log.error(me.toString());
            return Result.fail().message(me.getMessage());
        }
    }

    @ApiOperation(value = "查看文档列表")
    @PostMapping("queryListByCondition")
    public Result<DocumentQueryListVO> getDocumentListByCondition(
            @RequestBody @Valid ModuleConditionDTO conditionDTO
    ){
        DocumentQueryListVO documentInfoVOList = documentService.getListByCondition(conditionDTO);
        return Result.ok(documentInfoVOList);
    }

    @ApiOperation(value = "查看文档详情")
    @GetMapping("queryDocumentDetailById")
    public Result<DocumentDetailVO> getDocumentDetailByDocumentId(@RequestParam("documentId") Long documentId){
        DocumentDetailVO documentDetailVO = documentService.getDocumentDetailById(documentId);
        return Result.ok(documentDetailVO);
    }
}
